#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
from Common.stringManage import *
import time
import json
import os
import sys
import shutil


# 复制目录到指定路径
def copyDir(sourceDir, targetDir):
    if not os.path.isdir(sourceDir):
        print("源目录不存在")
        return "源目录不存在"
    if not os.path.exists(targetDir):
        os.makedirs(targetDir)
    listName = os.listdir(sourceDir)
    for name in listName:
        sourceAbs = os.path.join(sourceDir, name)
        targetAbs = os.path.join(targetDir, name)
        if os.path.isdir(sourceAbs):
            # 创建目标目录
            if not os.path.exists(targetAbs):
                os.makedirs(targetAbs)
            copyDir(sourceAbs, targetAbs)
        if os.path.isfile(sourceAbs):
            # 1.如果目标子级文件不存在  或者 目标子级文件存在但是该文件与原子级文件大小不一致   则需要复制
            if (not os.path.exists(targetAbs)) or (os.path.exists(targetAbs) and os.path.getsize(targetAbs) != os.path.getsize(sourceAbs)):
                rf = open(sourceAbs, mode="rb")
                wf = open(targetAbs, mode="wb")
                while True:
                    content = rf.read(1024*1024*10)
                    if len(content) == 0:
                        break
                    wf.write(content)
                    wf.flush()
                rf.close()
                wf.close()
